package kz.pompei.ifacer.util;

import static junit.framework.Assert.assertEquals;

import java.awt.Color;
import java.util.ArrayList;
import java.util.List;

import org.junit.Test;

public class RichTextTest {
  
  @Test
  public void fillIndexes_1() throws Exception {
    final List<Integer> openIndexes = new ArrayList<>();
    final List<Integer> closeIndexes = new ArrayList<>();
    
    String richText = "11{asd} sda {dsa {hello}} \\{df \\} as {dfds} af dsa fdsa fd";
    RichText.fillIndexes(openIndexes, closeIndexes, richText);
    
    assertEquals("[2, 12, 37]", openIndexes.toString());
    assertEquals("[6, 24, 42]", closeIndexes.toString());
    
    {
      int i = 0;
      assertEquals("asd", richText.substring(openIndexes.get(i) + 1, closeIndexes.get(i)));
    }
    {
      int i = 1;
      assertEquals("dsa {hello}", richText.substring(openIndexes.get(i) + 1, closeIndexes.get(i)));
    }
    {
      int i = 2;
      assertEquals("dfds", richText.substring(openIndexes.get(i) + 1, closeIndexes.get(i)));
    }
  }
  
  @Test
  public void fillIndexes_2() throws Exception {
    final List<Integer> openIndexes = new ArrayList<>();
    final List<Integer> closeIndexes = new ArrayList<>();
    
    String richText = "asd *fdsfds* {fdsf * / _ ~ dgfd} _fgdgfdsgfdsg_ /fdsfadasd/ ~fdsfsdfd~ ";
    RichText.fillIndexes(openIndexes, closeIndexes, richText);
    
    assertEquals("[4, 11, 13, 33, 46, 48, 58, 60, 69]", openIndexes.toString());
    assertEquals("[4, 11, 31, 33, 46, 48, 58, 60, 69]", closeIndexes.toString());
    
    String expected[] = new String[] { "*", "*", "{fdsf * / _ ~ dgfd", "_", "_", "/", "/", "~", "~" };
    
    for (int i = 0, C = openIndexes.size(); i < C; i++) {
      int from = openIndexes.get(i);
      int to = closeIndexes.get(i);
      if (to == from) to++;
      assertEquals(expected[i], richText.substring(from, to));
    }
  }
  
  @Test
  public void strToColor() throws Exception {
    {
      Color color = Util.strToColor("#aabbcc");
      assertEquals("java.awt.Color[r=170,g=187,b=204]", color.toString());
    }
    {
      Color color = Util.strToColor("   rEd ");
      assertEquals("java.awt.Color[r=255,g=0,b=0]", color.toString());
    }
    {
      Color color = Util.strToColor("grEen ");
      assertEquals("java.awt.Color[r=0,g=255,b=0]", color.toString());
    }
    {
      Color color = Util.strToColor("rgb 23 ,45, 89 ");
      assertEquals("java.awt.Color[r=23,g=45,b=89]", color.toString());
    }
  }
  
  @Test
  public void parse_1() throws Exception {
    RichTextDefStyle ds = new RichTextDefStyle();
    ds.fontSize = 17f;
    RichText rt = RichText.parse(ds, "START*dsadsad*Udfg _dsadsa_ gfdsgfdsg");
    
    int i = 0;
    
    assertEquals("{Default/17.0 rgb(0,0,0) }[START]", rt.parts.get(i++).toString());
    assertEquals("{Default/17.0 rgb(0,0,0) B}[dsadsad]", rt.parts.get(i++).toString());
    assertEquals("{Default/17.0 rgb(0,0,0) }[Udfg ]", rt.parts.get(i++).toString());
    assertEquals("{Default/17.0 rgb(0,0,0) U}[dsadsa]", rt.parts.get(i++).toString());
    assertEquals("{Default/17.0 rgb(0,0,0) }[ gfdsgfdsg]", rt.parts.get(i++).toString());
    
    assertEquals(i, rt.parts.size());
  }
  
  @Test
  public void parse_2() throws Exception {
    RichTextDefStyle ds = new RichTextDefStyle();
    ds.fontSize = 17f;
    RichText rt = RichText.parse(ds, "start {s34}wowKiss{fASD}{cRGB 1,1,1}"
        + "Hello world*point/cursive");
    
    int i = 0;
    
    assertEquals("{Default/17.0 rgb(0,0,0) }[start ]", rt.parts.get(i++).toString());
    assertEquals("{Default/34.0 rgb(0,0,0) }[wowKiss]", rt.parts.get(i++).toString());
    assertEquals("{ASD/34.0 rgb(1,1,1) }[Hello world]", rt.parts.get(i++).toString());
    assertEquals("{ASD/34.0 rgb(1,1,1) B}[point]", rt.parts.get(i++).toString());
    assertEquals("{ASD/34.0 rgb(1,1,1) BI}[cursive]", rt.parts.get(i++).toString());
    
    assertEquals(i, rt.parts.size());
  }
  
  @Test
  public void compile_1() throws Exception {
    String richText = "wow {s34}wowKiss{crgb 1,1,1}{fASD}Hello world*point/cursive";
    RichText rt = RichText.parse(richText);
    
    assertEquals(richText, rt.compile());
  }
  
  @Test
  public void compile_2() throws Exception {
    String richText = "one {c#112233}two";
    RichText rt = RichText.parse(richText);
    
    assertEquals("one {crgb 17,34,51}two", rt.compile());
  }
  
  @Test
  public void compile_3() throws Exception {
    String richText = "simple text";
    RichText rt = RichText.parse(richText);
    
    assertEquals(richText, rt.compile());
  }
  
}
